@extends('layout')
@section('title')
{{trans('app.manage_page_post')}}
@stop

@section('header')
<link rel="stylesheet" href="{{ URL::to('assets/js/plugins/datepicker/datepicker.css') }}">
<link rel="stylesheet" href="{{ URL::to('assets/js/plugins/timepicker/bootstrap-timepicker.css') }}">
@stop

@section('content')
<script type="text/javascript">
	$(function() {
		$('#frm-create-pagepost').submit(function() {
			if($('#fbpage').val() == null) {
				alert('{{trans('app.please_choose_fanpage')}}');
				return false;
			}

			if(($('#message').val() == '' && $('#status').val() == '') && $('#link').val() == '') {
				alert('{{trans('app.required_either_link_or_msg')}}');
				return false;
			}

			return true;
		});
		$("#link").change(function() {
			if($(this).val() != '') {
				$('.related-link').fadeIn('fast').show();
			}
			else {
				$('.related-link').fadeOut('fast').hide();
			}
		});
	})
</script>
@if(Session::get('result'))
<div class="alert alert-info">
	<a class="close" data-dismiss="alert" href="#" aria-hidden="true">×</a>
	<?php $result = Session::get('result'); ?>
	@foreach($result as $res)
	<li>{{ isset($res['error']['message']) ? $res['error']['message'] : '<a href="https://facebook.com/' . $res . '" target="_blank">'.trans('app.post_created').' <strong>' . $res . '</strong></a>' }}</li>
	@endforeach
</div>
@endif

@if(Session::get('error'))
<div class="alert alert-danger">
  <a class="close" data-dismiss="alert" href="#" aria-hidden="true">×</a>
  <strong>{{trans('app.error')}}!</strong> {{Session::get('error')}}.
</div>
@endif
<?php if( isset($input) && is_array($input)) extract($input) ?>
<div class="row">
	<div class="col-sm-5">

          <div class="portlet">

            <div class="portlet-header">

              <h3>
                <i class="fa fa-bullseye"></i>
                {{trans('app.create_page_post')}}
              </h3>

            </div> <!-- /.portlet-header -->
            <div class="portlet-content">
			<form id="frm-create-pagepost" method="post" action="{{ URL::to('create-page-post') }}" enctype="multipart/form-data" data-validate="parsley" class="form parsley-form">
				<div class="form-group">  
                  <label for="fbpage">{{trans('app.selectfbpage')}}</label>
                  <select id="fbpage" name="fbpage[]" class="form-control" data-required="true" multiple="true">
                    @if(!$pages->isEmpty())
                    	@foreach($pages as $page)
                    	<option value="{{ $page->fb_page_id }}" <?php echo (isset($fbpage) && in_array($page->fb_page_id, $fbpage)) ? ' selected' : '' ?>>{{{ $page->page_name }}}</option>
                    	@endforeach
                    @endif
                  </select>
                </div>
	            <ul id="pagePostTab" class="nav nav-tabs">
					<li class="{{(Input::get('tab') == 'pp') ? 'active' : ''}}">
			            <a href="#pp" data-toggle="tab"><i class="fa fa-pencil-square"></i> Status</a>
			        </li>
					<li class="{{(Input::get('tab') == 'pf') ? 'active' : ''}}">
			            <a href="#pf" data-toggle="tab"><i class="fa fa-picture-o"></i> {{trans('app.photo')}}</a>
			        </li>
				</ul>
				<div id="page-post" class="tab-content">
					<div class="tab-pane fade {{(Input::get('tab') == 'pp') ? 'in active' : ''}}" id="pp">
						<div>
							<div class="alert alert-info">
								<span class="glyphicon glyphicon-info-sign"> </span> {{trans('app.only_required_link_or_msg')}}.
							</div>
						</div>

		                <div class="form-group">
		                  <label for="message">{{trans('app.message')}}</label>
		                  <textarea data-minlength="5" name="message" id="message" cols="10" rows="2" class="form-control parsley-validated"><?php echo isset($message) ? $message : '' ?></textarea>
		                </div>

						<div class="form-group">  
		                  <label for="fbpage">Link</label>
		                      <input type="text" id="link" name="link" value="<?php echo isset($link) ? $link : '' ?>" class="form-control">
						</div>
						<div class="well related-link">
							<div class="form-group related-link">  
			                  <label for="picture">{{trans('app.link_picture')}}</label>
			                      <input type="text" id="picture" name="picture" value="<?php echo isset($picture) ? $picture : '' ?>" class="form-control">
							</div>
							<div class="form-group related-link">  
			                  <label for="name">{{trans('app.link_name')}}</label>
			                      <input type="text" id="name" name="name" value="<?php echo isset($name) ? $name : '' ?>" class="form-control">
							</div>
							<div class="form-group related-link">  
			                  <label for="caption">{{trans('app.link_caption')}}</label>
			                      <input type="text" id="caption" name="caption" value="<?php echo isset($caption) ? $caption : '' ?>" class="form-control">
							</div>
							<div class="form-group related-link">  
			                  <label for="desc">{{trans('app.link_desc')}}</label>
			                      <input type="text" id="desc" name="desc" value="<?php echo isset($desc) ? $desc : '' ?>" class="form-control">
							</div>
						</div>
		            </div>

	            	<div class="tab-pane fade {{(Input::get('tab') == 'pf') ? 'in active' : ''}}" id="pf">
		                <div class="form-group">
		                  <label for="message">{{trans('app.message')}}</label>
		                  <textarea data-minlength="5" name="status" id="status" cols="10" rows="2" class="form-control parsley-validated"><?php echo isset($status) ? $status : '' ?></textarea>
		                </div>
		                <div class="form-group">
		                	<label for="message">{{trans('app.photo')}}</label>
							<input type="file" id="photo" name="photo" class="form-control parsley-validated">
		                </div>
		            </div>
	            </div>
				<div class="well">
                	<div class="form-group">
	                  <label for="pdate">{{trans('app.publish_date')}}</label>
	                  <div class="input-group date ui-datepicker">
	                      <input id="pdate" name="pdate" value="{{ date('m/d/Y') }}" class="form-control parsley-validated" type="text" data-required="true">
	                      <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
	                  </div>
	                </div>
					
	                <div class="form-group">
	                  <label for="ptime">{{trans('app.publish_time')}}</label>
		               	<div class="input-group bootstrap-timepicker">
	                      <input id="ptime" name="ptime" class="form-control ui-timepicker" data-value="" type="text" data-required="true">
	                      <span class="input-group-addon"><i class="fa fa-clock-o"></i></span>
	                  	</div>
	                </div>
               </div>
	            <div class="form-group">
	              <button type="submit" class="btn btn-primary"><i class="fa fa-pencil-square-o"></i> {{trans('app.btn_post')}}</button>
	            </div>
				</form>
            </div> <!-- /.portlet-content -->

          </div> <!-- /.portlet -->
          
        </div> <!-- /.col -->
        <div class="col-sm-7">
        	<div class="portlet">

	            <div class="portlet-header">

	              <h3>
	                <i class="fa fa-list-alt"></i>
	                {{trans('app.recent_page_post')}}
	              </h3>

	            </div> <!-- /.portlet-header -->
	            <div class="portlet-content" style="text-align:center;">
				@if(!$pageposts->isEmpty())
				<table class="table table-striped table-bordered table-pagepost"
                data-display-rows="200"
                data-info="true"
                data-search="false"
                data-length-change="true"
                data-paginate="false">
					<thead>
	                    <tr>
	                      <th data-filterable="false" data-sortable="true">
	                        {{trans('app.page_name')}}
	                      </th>
	                      <th data-filterable="false" data-sortable="true">
	                        {{trans('app.msg_link')}}
	                      </th>
	                      <th data-filterable="false" data-sortable="true">
	                        {{trans('app.result')}}
	                      </th>
	                    </tr>
	                </thead>
					<tbody>
					@foreach($pageposts as $post)
					<tr>
						<td><a href="https://facebook.com/{{$post->fb_page_id}}" target="_blank">{{$post->page_name}}</a></td>
						<td><?php echo $post->post_message != '' ? str_limit($post->post_message, 55) : str_limit($post->post_url, 55) ?></td>
						<td>{{ $post->fb_post_id != '' ? '<a href="https://facebook.com/'.$post->fb_post_id.'" title="'.trans('app.open_in_fb').'" class="pagepost_rs_link" target="_blank"><span class="label label-success">Success <i class="fa fa-external-link-square"></i></span></a>' : '<span class="label label-warning">Error</span>'}}</td>
					</tr>
					@endforeach
					</tbody>
				</table>
				{{$pageposts->appends(array('tab' => $tab))->links()}}
				@endif
        		</div>
        	</div>
        </div>
</div> <!-- /.row -->
@stop

@section('footer')
<script src="{{ URL::to('assets/js/plugins/datatables/jquery.dataTables.min.js') }}"></script>
<script src="{{ URL::to('assets/js/plugins/datatables/DT_bootstrap.js') }}"></script>
<script src="{{ URL::to('assets/js/plugins/datepicker/bootstrap-datepicker.js') }}"></script>
<script src="{{ URL::to('assets/js/plugins/timepicker/bootstrap-timepicker.js') }}"></script>
@stop
